package com.ruoyi.purchase.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.ProcOrderDetail;
import com.ruoyi.purchase.service.IProcOrderDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.basedata.service.IBaseProductService;
import com.ruoyi.basedata.domain.BaseProduct;

/**
 * 采购订单明细Controller
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@Controller
@RequestMapping("/purchase/detail")
public class ProcOrderDetailController extends BaseController
{
    private String prefix = "purchase/detail";

    @Autowired
    private IProcOrderDetailService procOrderDetailService;
    
    @Autowired
    private IBaseProductService baseProductService;

    @RequiresPermissions("purchase:detail:view")
    @GetMapping()
    public String detail()
    {
        return prefix + "/detail";
    }

    /**
     * 查询采购订单明细列表
     */
    @RequiresPermissions("purchase:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcOrderDetail procOrderDetail)
    {
        startPage();
        List<ProcOrderDetail> list = procOrderDetailService.selectProcOrderDetailList(procOrderDetail);
        return getDataTable(list);
    }

    /**
     * 导出采购订单明细列表
     */
    @RequiresPermissions("purchase:detail:export")
    @Log(title = "采购订单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcOrderDetail procOrderDetail)
    {
        List<ProcOrderDetail> list = procOrderDetailService.selectProcOrderDetailList(procOrderDetail);
        ExcelUtil<ProcOrderDetail> util = new ExcelUtil<ProcOrderDetail>(ProcOrderDetail.class);
        return util.exportExcel(list, "采购订单明细数据");
    }

    /**
     * 新增采购订单明细
     */
    @RequiresPermissions("purchase:detail:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // 查询所有可用商品列表
        BaseProduct baseProduct = new BaseProduct();
        baseProduct.setStatus("0"); // 0表示正常状态
        List<BaseProduct> productList = baseProductService.selectBaseProductList(baseProduct);
        mmap.put("productList", productList);
        return prefix + "/add";
    }

    /**
     * 新增保存采购订单明细
     */
    @RequiresPermissions("purchase:detail:add")
    @Log(title = "采购订单明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcOrderDetail procOrderDetail)
    {
        return toAjax(procOrderDetailService.insertProcOrderDetail(procOrderDetail));
    }

    /**
     * 修改采购订单明细
     */
    @RequiresPermissions("purchase:detail:edit")
    @GetMapping("/edit/{detailId}")
    public String edit(@PathVariable("detailId") Long detailId, ModelMap mmap)
    {
        ProcOrderDetail procOrderDetail = procOrderDetailService.selectProcOrderDetailByDetailId(detailId);
        mmap.put("procOrderDetail", procOrderDetail);
        
        // 查询所有可用商品列表
        BaseProduct baseProduct = new BaseProduct();
        baseProduct.setStatus("0"); // 0表示正常状态
        List<BaseProduct> productList = baseProductService.selectBaseProductList(baseProduct);
        mmap.put("productList", productList);
        
        return prefix + "/edit";
    }

    /**
     * 修改保存采购订单明细
     */
    @RequiresPermissions("purchase:detail:edit")
    @Log(title = "采购订单明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcOrderDetail procOrderDetail)
    {
        return toAjax(procOrderDetailService.updateProcOrderDetail(procOrderDetail));
    }

    /**
     * 删除采购订单明细
     */
    @RequiresPermissions("purchase:detail:remove")
    @Log(title = "采购订单明细", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(procOrderDetailService.deleteProcOrderDetailByDetailIds(ids));
    }
    
    /**
     * 获取商品列表
     */
    @GetMapping("/productList")
    @ResponseBody
    public AjaxResult getProductList()
    {
        BaseProduct baseProduct = new BaseProduct();
        baseProduct.setStatus("0"); // 0表示正常状态
        List<BaseProduct> productList = baseProductService.selectBaseProductList(baseProduct);
        return AjaxResult.success(productList);
    }
}
